package com.qckj.amz.domain.spapi.feeds.home.ProductFeedXml;

import com.qckj.amz.domain.ProductFmlBasic;
import com.qckj.amz.domain.ProductFmlDesc;
import com.qckj.amz.domain.ProductFmlRlat;
import com.qckj.amz.domain.ProductInit;
import com.qckj.amz.domain.spapi.feeds.home.AmazonEnvelope;
import com.qckj.amz.domain.spapi.feeds.home.Header;
import com.qckj.amz.domain.spapi.feeds.home.Message;
import com.qckj.amz.utils.constants.AmzConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author luchuan
 * @version v1.0
 * @date 2022/11/12 0012
 **/
public class CreateProductFeedXml {

    /**
     *
     * @param sellerId          卖家记号
     * @param productInit       产品常用属性
     * @param listProduct       产品信息
     * @return
     */
    public String getProductFeedXml(String sellerId, ProductInit productInit, List<Map<String, Object>> listProduct) {

        if (listProduct==null && listProduct.isEmpty()){
            return null;
        }

        ArrayList<Message> messageList = new ArrayList<>();//封装多个Message，即多个sku产品，无论是有无变体或多个不同的产品
        for (Map<String, Object> mapProduct: listProduct) {
            ProductFmlBasic basicObj = (ProductFmlBasic)mapProduct.get("basicObj");
            List<ProductFmlRlat> rlatList = (List<ProductFmlRlat>)mapProduct.get("rlatList");
            ProductFmlDesc descObj = (ProductFmlDesc)mapProduct.get("descObj");

            //封装多个变体
            for (ProductFmlRlat rlat:rlatList) {

                /**
                 * 八级/VariationData
                 * 当二级Message为父体时，只赋值VariationTheme，若为子体时，需要setSize()和setColor()
                 */
                VariationData variationData = new VariationData();
                variationData.setVariationTheme(rlat.getVariationTheme());//Size-Color
                variationData.setSize(rlat.getSizeName());
                variationData.setColor(rlat.getColorName());

                /**
                 * 七级/Home
                 */
                Home c_home = new Home();
                c_home.setVariationData(variationData);

                /**
                 * 六级/ProductType
                 */
                ProductType productType = new ProductType();
                productType.setHome(c_home);

                /**
                 * 五级/Home
                 */
                Home p_home = new Home();
                p_home.setProductType(productType);
                p_home.setParentage(rlat.getParentChild());//父体Parent
                //p_home.setMaterial("suliao");//材质

                //封装五点描述/BulletPoint
                List<String> bulletPointList = new ArrayList<>();
                bulletPointList.add(descObj.getBulletPoint1());//Example Bullet Point 1
                bulletPointList.add(descObj.getBulletPoint2());//Example Bullet Point 2
                bulletPointList.add(descObj.getBulletPoint3());//Example Bullet Point 3
                bulletPointList.add(descObj.getBulletPoint4());//Example Bullet Point 4
                bulletPointList.add(descObj.getBulletPoint5());//Example Bullet Point 5

                /**
                 * 四级/ProductData
                 */
                ProductData productData = new ProductData();
                productData.setHome(p_home);

                /**
                 * 四级/StandardProductID
                 */
                StandardProductID standardProductID = new StandardProductID();
                standardProductID.setType(rlat.getExternalProductIdType()==null ? "" : rlat.getExternalProductIdType());//"EAN"
                standardProductID.setValue(rlat.getExternalProductId()==null ? "" : rlat.getExternalProductId());//"EAN123456"

                /**
                 * 四级/Condition
                 */
                Condition condition = new Condition();
                condition.setConditionType(productInit.getConditionType());//New

                /**
                 * 四级/DescriptionData
                 */
                DescriptionData descriptionData = new DescriptionData();
                descriptionData.setTitle(descObj.getItemName());//Example Product Title
                descriptionData.setBrand(productInit.getBrandName());//Brand QIAOCHUANKEJI
                descriptionData.setDescription(descObj.getProductDescription()==null ? "" : descObj.getProductDescription().replaceAll("\n","<br>"));//描述-This is an example product description.
                descriptionData.setManufacturer(productInit.getManufacturer());//制造商-Manufacturer QIAOCHUANKEJI
                descriptionData.setMfrPartNumber(productInit.getBrandName()+"-"+basicObj.getProductCode());//制造商零件编号-MfrPartNumber 12345
                descriptionData.setSearchTerms(descObj.getGenericKeywords());//关键字-SearchTerms/keyword

                descriptionData.setRecommendedBrowseNode(descObj.getRecommendedBrowseNode());//12345678
                descriptionData.setCountryOfOrigin(productInit.getCountryOfOrigin());//CN
                descriptionData.setBulletPoint(bulletPointList);

                /**
                 * 三级/Product
                 */
                Product product = new Product();
                product.setSKU(rlat.getItemSku());//sku123456
                product.setStandardProductID(standardProductID);
                product.setCondition(condition);
                product.setDescriptionData(descriptionData);
                product.setProductData(productData);

                /**
                 * 二级/Message,此节点为List对象，可以包含主体、变体
                 */
                Message message = new Message();
                message.setMessageID(basicObj.getId());
                message.setOperationType(productInit.getUpdateDelete());//Update、delete
                message.setProduct(product);
                messageList.add(message);

            }

        }

        /**
         * 二级/Header
         */
        Header header = new Header();
        header.setDocumentVersion("1.01");
        header.setMerchantIdentifier(sellerId);

        /**
         * 一级/根节点AmazonEnvelope
         */
        AmazonEnvelope amazonEnvelope = new AmazonEnvelope();
        amazonEnvelope.setHeader(header);
        amazonEnvelope.setMessageType("Product");//Product
        amazonEnvelope.setPurgeAndReplace("false");
        amazonEnvelope.setMessage(messageList);

        /**
         * Javabean对象转换xml字符串
         */
        String xmlStr = AmzCommon.JavabeanToXmlStr(AmzConstants.FEED_TYPE_PRODUCT_FEED, amazonEnvelope);

        return xmlStr;
    }


}
